/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    gongwenkai
 * 创建时间:    2023-09-27
 */
package org.jeecg.modules.flow.callback.outbound.order;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.business.doc.outbound.entity.WmsOutReceipt;
import org.jeecg.modules.business.doc.outbound.service.IWmsOutReceiptService;
import org.jeecg.modules.business.outbound.service.IWmsOutReceiptTaskService;
import org.jeecg.modules.business.third.service.ISapOperateService;
import org.jeecg.modules.dock.entity.dto.response.IngredientsGetResponseDTO;
import org.jeecg.modules.dock.entity.dto.response.ProductReceptionResponseDTO;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.callback.enums.SimpleDocumentTypeEnum;
import org.jeecg.modules.flow.callback.enums.TransferStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.jeecg.modules.flow.sys.exception.FlowCurrentFailException;
import org.jeecg.modules.flow.sys.service.WorkFlowService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 辅材领用出库单过账 回调bean
 *
 * @author gongwenkai
 * @version 1.0.0
 * @since 2023-09-27
 */
@RequiredArgsConstructor
@Component
public class C51OutBoundReceiptTransferBean<T> extends BaseCustomBeanRun<T> {

    private final IWmsOutReceiptService wmsOutReceiptService;

    private final IWmsOutReceiptTaskService wmsOutReceiptTaskService;

    private final WorkFlowService workFlowService;

    private final ISapOperateService sapOperateService;

    /**
     * 业务逻辑 保存或更新出库单
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String receiptId = baseFlowDto.getInfoId();
        WmsOutReceipt wmsOutReceipt = wmsOutReceiptService.getById(receiptId);
        if(Objects.isNull(wmsOutReceipt)){
            throw new JeecgBootException("出库单不存在");
        }
        if (DocumentStatusEnum.CLOSE.getCode().equals(wmsOutReceipt.getDocumentStatus())) {
            throw new JeecgBootException("提交失败，所属出库任务已关闭");
        }
        Boolean successFlag=true;
        //调用第三方接口
        List<IngredientsGetResponseDTO> responseList=new ArrayList<>();
        try{
            responseList = sapOperateService.outboundByOrder(receiptId);
        }catch (Exception e){
            successFlag=false;
            baseFlowDto.setResult(Result.error(e.getMessage()));
            //设置过账状态 过账失败
            wmsOutReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.OUT_RECEIPT,null,receiptId, TransferStatusEnum.TRANSFER_FAIL);

        }

        if(!CollectionUtil.isEmpty(responseList)){
            for(IngredientsGetResponseDTO responseDTO:responseList){
                if("E".equals(responseDTO.getResult())){
                    successFlag=false;
                    baseFlowDto.setResult(Result.error(responseDTO.getMessage()));
                    //设置过账状态 过账失败
                    wmsOutReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.OUT_RECEIPT,null,receiptId, TransferStatusEnum.TRANSFER_FAIL);
                }
            }
        }
        if(successFlag){
            //设置过账状态 过账成功
            wmsOutReceiptTaskService.updateTransferStatus(SimpleDocumentTypeEnum.OUT_RECEIPT,null,receiptId, TransferStatusEnum.TRANSFER_SUCCESS);
            // 当前前端调用业务需要你返回的
            baseFlowDto.setResult(Result.OK());
        }
        // id是保存业务记录的id。用于生成业务流程信息记录
        baseFlowDto.setInfoId(receiptId);
    }
}
